package xserver;

import org.apache.mina.common.IoSession;

import xgame.IXsession;

/**
 * 游戏会话, 使用适配器模式包装了 IoSession 接口
 * 
 * @author AfritXia 
 * @version $Rev: 17 $
 *
 */
public class XgameSession implements IXsession {
	/**
	 * 会话 Id
	 * 
	 */
	public static final String SESSION_ID = "__sessionId";

	/**
	 * IO 会话对象
	 * 
	 */
	private IoSession _ioSession = null;

	/**
	 * 类参数构造器
	 * 
	 * @param session
	 * @throws IllegalArgumentException if session == null 
	 * 
	 */
	public XgameSession(IoSession session) {
		if (session == null) {
			throw new IllegalArgumentException("session is null");
		}

		this._ioSession = session;
	}

	@Override
	public long getId() {
		Object objId = this._ioSession.getAttribute(SESSION_ID);

		if (objId == null) {
			return 0L;
		}

		return (Long)objId;
	}

	@Override
	public Object getAttribute(String key) {
		if (key == null) {
			return null;
		}

		return this._ioSession.getAttribute(key);
	}

	@Override
	public void putAttribute(String key, Object value) {
		if (key == null || value == null) {
			return;
		}

		this._ioSession.setAttribute(key, value);
	}

	/**
	 * 获取 IO 会话
	 * 
	 * @return 
	 * 
	 */
	IoSession getIoSession() {
		return this._ioSession;
	}
}
